package com.anpy.security;

import com.anpy.security.service.SysUserService;
import cn.hutool.core.util.ObjectUtil;
import com.anpy.common.exception.ExceptionResponse;
import com.anpy.security.domain.SysUserEntity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * 自定义登录逻辑
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException  {
        SysUserEntity userEntity = sysUserService.getByUserName(username);
        if(ObjectUtil.isNull(userEntity)){
            ExceptionResponse.assertError("用户不存在");
        }
        return new User(userEntity.getUserName(),userEntity.getPassword(),new ArrayList<>());
    }
}
